/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package JavaMailApp.controller;

import java.util.Properties;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.PasswordAuthentication;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;

/**
 *
 * @author Emmanuel
 */
public class JavaMailController {

    private String remitente;
    private String destinatario;
    private String asunto;
    private String mensaje;

    public JavaMailController() {
    }

    public String enviarCorreo() {
        try {
            String smtp = "smtp.gmail.com";
            Properties props = new Properties();
            props.setProperty("mail.transport.protocol","smtp");
            props.setProperty("mail.host", smtp);
            props.put("mail.smtp.auth", "true");
            props.put("mail.smtp.port", "465");
            props.put("mail.smtp.socketFactory.port", "465");
            props.put("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory");
            props.put("mail.smtp.socketFactory.fallback", "false");
            props.setProperty("mail.smtp.quitwait", "false");

            Session session = Session.getInstance(props,
                    new javax.mail.Authenticator() {

                        @Override
                        protected PasswordAuthentication getPasswordAuthentication() {
                            return new PasswordAuthentication("quickdunk@gmail.com", "QuickDunkX");
                        }
                    });
            MimeMessage message = new MimeMessage(session);
            message.setSender(new InternetAddress("quickdunk@gmail.com"));
            message.setSubject("Hola Mundo");
            message.setContent("Este es un mensaje enviado desde una aplicación Java.", "text/plain");
            message.setRecipient(Message.RecipientType.TO, new InternetAddress("wcumpan@hotmail.com"));
            Transport.send(message);

            return "";
        } catch (MessagingException ex) {
            ex.printStackTrace();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return "";
    }

    public String enviarCorreoPreConfigruado(){
        try {
            final Properties p = getMailProperties();
            Session session = Session.getInstance(p,
                    new javax.mail.Authenticator() {

                        @Override
                        protected PasswordAuthentication getPasswordAuthentication() {
                            return new PasswordAuthentication(p.getProperty("user"), p.getProperty("password").toString());
                        }
                    });
            MimeMessage message = new MimeMessage(session);
            message.setSender(new InternetAddress("quickdunk@gmail.com"));
            message.setSubject("Prueba");
            message.setContent("Este es un mensaje enviado desde una aplicación Java.", "text/plain");
            message.setRecipient(Message.RecipientType.TO, new InternetAddress("wcumpan@hotmail.com"));
            Transport.send(message);
            return "";
        } catch (MessagingException ex) {
            ex.printStackTrace();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return "";
    }

    public Properties getMailProperties() throws Exception {
        Properties propiedades = new Properties();
        try {
            propiedades.load(JavaMailController.this.getClass().getResourceAsStream("/JavaMailApp/controller/MailOptions.properties"));
            return propiedades;
        } catch (Exception e) {
            return null;
        }
    }

    public String getAsunto() {
        return asunto;
    }

    public void setAsunto(String asunto) {
        this.asunto = asunto;
    }

    public String getDestinatario() {
        return destinatario;
    }

    public void setDestinatario(String destinatario) {
        this.destinatario = destinatario;
    }

    public String getMensaje() {
        return mensaje;
    }

    public void setMensaje(String mensaje) {
        this.mensaje = mensaje;
    }

    public String getRemitente() {
        return remitente;
    }

    public void setRemitente(String remitente) {
        this.remitente = remitente;
    }
}
